import xFetch from './xFetch';

const authURL = "/auth/jjOAuthv1.0";

export async function auth(code) {

  const client_id = "7af5f492a74499e7";
  // const client_id = "55e79c13403cd1c8";
  const callbackUrl = "http://192.168.18.71:8989/callback";
  // const callbackUrl = "http://192.168.8.198:8989/callback";

  const token_url = authURL + "/token.php";

  const client_secret = "f5ff8eee1bcdff53e040a48b6ecd1cbe99d7fec2";

  let formData = new FormData();
  formData.append("client_id", client_id);
  formData.append("client_secret", client_secret);
  formData.append("grant_type", "authorization_code");
  formData.append("code", code);
  formData.append("redirect_uri", callbackUrl);

  return xFetch(token_url,
    {
      method: 'POST',
      body: formData,
    })
}

export async function userInfo(token) {

  const resource_url = authURL + "/resource.php";

  return xFetch(resource_url + '?access_token=' + token,
    {
      method: 'GET',
    }).then(function (res) {
    return res.json();
  })
}

export async function userCheck(username) {

  return xFetch('/api/user/' + username + '/check', {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
    }
  })
}
